@isTest
private class OppProductToReservation_TestClass 
{
    static testMethod void oppToReservations() 
    {
        Opportunity o = new Opportunity(name='Opp1231Sample', closedate = Date.today(), StageName = 'Closed Won');
        insert o;
//        Product2[] prod = [select id, name from Product2 limit 1];
//        PricebookEntry pbe = [select id, Product2Id  from PricebookEntry where Product2Id = :prod[0].Id limit 1];

		Pricebook2 s = [select id from Pricebook2 where IsStandard = true];   
		    
	    // create the product
	    Product2 p1 = new Product2(
	        name='Product test1232',
	        IsActive=true,
	        Description='Product',
	        ProductCode='123451'
	    );
	    insert p1;       
	   
	    // create the pricebookentry
	    PricebookEntry pbe = new PricebookEntry(
	        Pricebook2Id=s.id,
	        Product2Id=p1.id,
	        UnitPrice=0.00,
	        IsActive=true,
	        UseStandardPrice=false
	    );
	    insert pbe;           

        OpportunityLineItem[] oliList = new OpportunityLineItem[]{};
        for(Integer i = 0; i < 200; i++)
        {
            OpportunityLineItem oli = new OpportunityLineItem(Quantity = 1, PricebookEntryId=pbe.Id, OpportunityId=o.Id, TotalPrice=1000);        
            oliList.add(oli); 
        }
        insert oliList;
        
        for(OpportunityLineItem[] oliList2: [select quantity, make_good__c from OpportunityLineItem where opportunityid = :o.id ])
        {
            for(opportunitylineitem oli2: oliList2)
            {
                oli2.make_good__c = true;
            }
            update oliList2;
        }
       /*
       OpportunityLineItem[] oliList = [select Opportunityid, Make_Good__c, pricebookentry.product2.id from OpportunityLineItem order by createddate desc limit 1];
       oliList[0].make_good__c = false;
       update oliList; 
       */
       
      /* for(OpportunityLineItem oli: oliList)
       {
           oli.Make_good__c = true;
       }
       upsert oliList;*/
    }

    static testMethod void deleteOLI() 
    {
        Opportunity o = new Opportunity(name='Opp1231Sample1', closedate = Date.today(), StageName = 'Closed Won');
        insert o;
//        Product2[] prod = [select id, name from Product2 limit 1];
//        PricebookEntry pbe = [select id, Product2Id  from PricebookEntry where Product2Id = :prod[0].Id limit 1];
        
		Pricebook2 s = [select id from Pricebook2 where IsStandard = true];   
		    
	    // create the product
	    Product2 p1 = new Product2(
	        name='Product test1231',
	        IsActive=true,
	        Description='Product',
	        ProductCode='123451'
	    );
	    insert p1;       
	   
	    // create the pricebookentry
	    PricebookEntry pbe = new PricebookEntry(
	        Pricebook2Id=s.id,
	        Product2Id=p1.id,
	        UnitPrice=0.00,
	        IsActive=true,
	        UseStandardPrice=false
	    );
	    insert pbe;           
    
        
        OpportunityLineItem[] oliList = new OpportunityLineItem[]{};
        for(Integer i = 0; i < 1; i++)
        {
            OpportunityLineItem oli = new OpportunityLineItem(Quantity = 1, PricebookEntryId=pbe.Id, OpportunityId=o.Id, TotalPrice=1000);        
            oliList.add(oli); 
        }
        insert oliList;
        oliList = [select quantity, make_good__c from OpportunityLineItem where opportunityid = :o.id ];
        
        delete oliList; 
    }

	static testMethod void testReservationScheduler()
	{
		Opportunity[] oList = new Opportunity[]{};
		for(Integer i=0; i<200;i++)
		{
        	Opportunity o = new Opportunity(name='OppSample1231'+i, closedate = Date.today(), StageName = 'Closed Won', Amount = 20000);
        	oList.add(o);
		}
		
		test.startTest();
        
        insert oList;
        oList.clear();
        
        oList = [select id from Opportunity order by createddate desc limit 200];
//        Product2[] prod = [select id, name from Product2 limit 1];
//        PricebookEntry pbe = [select id, Product2Id  from PricebookEntry where Product2Id = :prod[0].Id limit 1];

        
		Pricebook2 s = [select id from Pricebook2 where IsStandard = true];   
		    
	    // create the product
	    Product2 p1 = new Product2(
	        name='Product test1231',
	        IsActive=true,
	        Description='Product',
	        ProductCode='123451'
	    );
	    insert p1;       
	   
	    // create the pricebookentry
	    PricebookEntry pbe = new PricebookEntry(
	        Pricebook2Id=s.id,
	        Product2Id=p1.id,
	        UnitPrice=0.00,
	        IsActive=true,
	        UseStandardPrice=false
	    );
	    insert pbe;           
    
        OpportunityLineItem[] oliList = new OpportunityLineItem[]{};
        
        for(Opportunity o: oList)
        {
            OpportunityLineItem oli = new OpportunityLineItem(Quantity = 1, PricebookEntryId=pbe.Id, OpportunityId=o.Id, TotalPrice=1000);        
            oliList.add(oli); 
        }
        
        insert oliList;
        
        Reservations__c[] rList = [select id from reservations__c order by createddate desc limit 200];
        delete rList;

		Datetime dt = Datetime.now().addMinutes(10);
		String sch = '0 '+dt.minute()+' '+dt.hour()+' '+dt.day()+' '+dt.month()+' '+' ? '+dt.year();
		ReservationDiscrepancyCheckScheduler  rdcs = new ReservationDiscrepancyCheckScheduler();
		ID scheduleId = system.schedule('Reservation Scheduled Test '+sch, sch, rdcs);    
		
		ReservationDiscrepancyCheckBatch rcb = new ReservationDiscrepancyCheckBatch(scheduleId);
		Database.executeBatch(rcb, 200);
        
		test.stopTest();
	}
}